package com.starbucks.opencase.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.data.daPath;
import com.starbucks.opencase.entity.entGoodsInfo;
import com.starbucks.opencase.entity.entProperties;
import com.starbucks.opencase.function.funCarry;
import com.starbucks.opencase.function.funSend;
import com.starbucks.opencase.modualr.moPetCarry;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.tool.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class serUserScan {
    @Autowired
    private funSession funSession;
    @Autowired
    private toolUserScan toolUserScan;
    @Autowired
    private toolGoodsInfo toolGoodsInfo;
    @Autowired
    private toolUserGoods toolUserGoods;
    @Autowired
    private toolUserWallet toolUserWallet;
    @Autowired
    private toolUserPet toolUserPet;
    @Autowired
    private toolProperties toolProperties;
    @Autowired
    private toolUserInfo toolUserInfo;
    @Autowired
    private funCarry funCarry;
    @Autowired
    private funSend funSend;
    public void scan(String id, JsonNode message){
        ObjectNode on=new ObjectMapper().createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.scan.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(id,on))return;
        String uid= funSession.getUIdOfId(id);
        entProperties properties=toolProperties.queryInfo();
        long energy=toolUserWallet.queryEnergy(uid);
        int expendEnergy=properties.getExpendEnergy();
        if(energy<=expendEnergy){
            on.put(enumCMKey.tip.name(), enumCMTip.not_enough_energy.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        int num=0;
        double pro=new Random().nextDouble();
        if(pro<=properties.getProEnergy())num=0;
        else if(pro<=properties.getProGoods()+properties.getProEnergy())num=1;
        else if(pro<=properties.getProPet()+properties.getProGoods()+properties.getProEnergy())num=2;
        else if(pro<=properties.getProBusinessman()+properties.getProPet()+properties.getProGoods()+properties.getProEnergy())num=3;
        else if(pro<=properties.getProPlayer()+properties.getProBusinessman()+properties.getProPet()+properties.getProGoods()+properties.getProEnergy())num=4;
        on.put(enumCMKey.type.name(), num);
        switch (num){
            case 1->{//物品
                entGoodsInfo en= toolUserScan.randomGoods();
                on.put(enumCMKey.goods.name(),en.getGid());
                on.put(enumCMKey.name.name(),en.getName());
                on.put(enumCMKey.url.name(), daPath.getGoods(en.getGid()));
            }
            case 2->{//宠物
                String pid= toolUserPet.randomPetIdForNotHas(uid);
                if(pid.isEmpty()){
                    on.put(enumCMKey.tip.name(), enumCMTip.error_scan.getTip());
                    funSession.sendMessage(id,on);
                    return;
                }
                on.put(enumCMKey.pet.name(),pid);
                on.put(enumCMKey.name.name(),toolUserPet.queryName(pid));
                on.put(enumCMKey.url.name(), daPath.getPet(pid));
            }
            case 3->{//商人
                String uuid=toolUserInfo.randomBusinessman(uid);
                on.put(enumCMKey.uid.name(), uuid);
                on.put(enumCMKey.nickname.name(), toolUserInfo.queryNicknameOfUID(uuid));
                on.put(enumCMKey.url.name(), daPath.getAvatar(uuid));
            }
            case 4->{//附近的人
//                String uuid= toolUserInfo.randomPlayer(uid);
                String uuid=funSession.randomUID(uid);

                on.put(enumCMKey.uid.name(), uuid);
                on.put(enumCMKey.nickname.name(), toolUserInfo.queryNicknameOfUID(uuid));
                on.put(enumCMKey.url.name(), daPath.getAvatar(toolUserInfo.queryInfo(uuid).getGender()+""));
            }
            default -> {
                on.put(enumCMKey.energy.name(), properties.getProEnergyAward());
                on.put(enumCMKey.url.name(), "other/energy.png");
            }
        }
        toolUserWallet.subEnergy(uid,expendEnergy);
        on.put(enumCMKey.status.name(), true);
        on.put(enumCMKey.expendEnergy.name(), expendEnergy);
        funSend.expendEnergy(id,energy-expendEnergy,expendEnergy);
        funSession.sendMessage(id,on);
    }

    /**
     * 背回、领取
     * @param id
     * @param message
     */
    public void carry(String id,JsonNode message){
        long t=System.currentTimeMillis();
        ObjectNode on=new ObjectMapper().createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.carry.ordinal());
        on.put(enumCMKey.timestamp.name(), t);
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(id,on))return;
        if(!message.has(enumCMKey.type.name())){
            on.put(enumCMKey.tip.name(), enumCMTip.no_has_type.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        int type=message.get(enumCMKey.type.name()).asInt();
        String uid=funSession.getUIdOfId(id);
        on.put(enumCMKey.type.name(), type);
        switch (type){
            case 1->{
                if(!message.has(enumCMKey.gid.name())){
                    on.put(enumCMKey.tip.name(), enumCMTip.no_has_gid.getTip());
                    funSession.sendMessage(id,on);
                    return;
                }
                String gid=message.get(enumCMKey.gid.name()).asText();
                if(!message.has(enumCMKey.pid.name())){
                    on.put(enumCMKey.tip.name(), enumCMTip.no_has_pid.getTip());
                    funSession.sendMessage(id,on);
                    return;
                }
                String pid=message.get(enumCMKey.pid.name()).asText();
                if(funCarry.isCarry(uid,pid)){
                    on.put(enumCMKey.tip.name(),enumCMTip.not_pet_leisure.getTip());
                    funSession.sendMessage(id,on);
                    return;
                }
                entGoodsInfo en=toolGoodsInfo.queryInfo(message.get(enumCMKey.gid.name()).asText());
                moPetCarry carry=new moPetCarry();
                carry.setPid(pid);
                carry.setUid(uid);
                carry.setGid(gid);
                carry.setStart(t);
                carry.setEnd((long)en.getUseTime()*60*1000+ carry.getStart());
                funCarry.addCarry(carry);
                on.put(enumCMKey.useTime.name(),carry.getEnd()- carry.getStart());
                on.put(enumCMKey.endTime.name(), carry.getEnd());
                on.put(enumCMKey.tip.name(), enumCMTip.success_carry_start.getTip());
                on.put(enumCMKey.status.name(), true);
                funSession.sendMessage(id,on);
            }
            case 2->{
                if(!message.has(enumCMKey.pid.name())){
                    on.put(enumCMKey.tip.name(), enumCMTip.no_has_pid.getTip());
                    funSession.sendMessage(id,on);
                    return;
                }
                String pid=message.get(enumCMKey.pid.name()).asText();
                toolUserPet.add(uid,pid);
                on.put(enumCMKey.tip.name(), enumCMTip.success_draw.getTip());
                on.put(enumCMKey.status.name(), true);
                funSession.sendMessage(id,on);
            }
            case 3->{

            }
            case 4->{

            }
            default -> {
                if(!message.has(enumCMKey.energy.name())){
                    on.put(enumCMKey.tip.name(), enumCMTip.no_has_energy.getTip());
                    funSession.sendMessage(id,on);
                    return;
                }
                entProperties properties=toolProperties.queryInfo();
                toolUserWallet.addEnergy(uid, properties.getProEnergyAward());
                long all_energy= toolUserWallet.queryEnergy(uid);
                on.put(enumCMKey.status.name(), true);
                on.put(enumCMKey.energy.name(), all_energy);
                funSession.sendMessage(id,on);
                funSend.expendEnergy(id,all_energy,-properties.getProEnergyAward());
            }
        }
    }

    /**
     * 查询正在背回的物品
     * @param id
     * @param message
     */
    public void queryCarryGoods(String id,JsonNode message){
        ObjectNode on=new ObjectMapper().createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.queryPetCarryGoods.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        if(!funSession.isOnlineOfId(id,on))return;
        if(!message.has(enumCMKey.pid.name())){
            on.put(enumCMKey.status.name(), false);
            on.put(enumCMKey.tip.name(), enumCMTip.no_has_pid.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        String pid=message.get(enumCMKey.pid.name()).asText();
        String uid= funSession.getUIdOfId(id);
        String gid= funCarry.getGId(uid,pid);
        on.put(enumCMKey.goods.name(), gid);
        on.put(enumCMKey.url.name(), daPath.getGoods(gid));
        on.put(enumCMKey.name.name(), toolUserGoods.queryName(gid));
        on.put(enumCMKey.useTime.name(), toolUserGoods.queryUseTime(gid)*60*1000);
        on.put(enumCMKey.endTime.name(), funCarry.getEndTime(uid,pid));
        on.put(enumCMKey.status.name(), !gid.isEmpty());
        funSession.sendMessage(id,on);
    }

    /**
     * 领取运回的物品
     * @param id
     * @param message
     */
    public void carryDraw(String id,JsonNode message){
        ObjectNode on=new ObjectMapper().createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.carryDraw.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        if(!funSession.isOnlineOfId(id,on))return;
        if(!message.has(enumCMKey.pid.name())){
            on.put(enumCMKey.status.name(), false);
            on.put(enumCMKey.tip.name(), enumCMTip.no_has_pid.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        String pid=message.get(enumCMKey.pid.name()).asText();
        String uid=funSession.getUIdOfId(id);
        if(!funCarry.isCarry(uid,pid)){//没有正在运输
            on.put(enumCMKey.status.name(), false);
            on.put(enumCMKey.tip.name(), enumCMTip.not_carry.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        if(!funCarry.isCarryEnd(uid,pid)){
            on.put(enumCMKey.status.name(), false);
            on.put(enumCMKey.tip.name(), enumCMTip.not_carry_end.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        String gid= funCarry.getGId(uid,pid);
        toolUserGoods.addGoods(uid,gid,1);
        funCarry.remove(uid,pid);
        on.put(enumCMKey.status.name(), true);
        on.put(enumCMKey.tip.name(), enumCMTip.success_carry_draw_ok.getTip());
        funSession.sendMessage(id,on);
    }
}
